/*
  Copyright © 2010  Camilo Costa Campos

      This file is part of Touché.

      Touché is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published
      by the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */


//Tags used in Touche:
//<ToucheText> "Text message to the other users" </ToucheText>
//<ToucheObject> </ToucheObject>
//<StartEditing> objId = int Id </ToucheStartEditing>
//<ToucheFinishEditing> objId = int Id </ToucheFinishEditing>
//

#include "interpreter.h"
#include <QXmlStreamReader>
#include <QXmlStreamAttributes>
#include <iostream>

Interpreter::Interpreter()
{
}

void Interpreter::onMessageReceived(const QXmppMessage &msg)
{
    // TODO: remove qDebug
    qDebug("Hello World");
    QXmlStreamAttribute atr;
    QXmlStreamAttributes atrs;
    QXmlStreamReader xml(msg.body());


    QString name, value, text, outro, namespaceUri, dtdName, qualifiedName;

    while(!xml.atEnd())
    {
        atrs = xml.attributes();

        switch(xml.readNext()){
        case QXmlStreamReader::Invalid:
            qDebug("erro encontrado");
            break;
        case QXmlStreamReader::StartDocument:
        case QXmlStreamReader::EndDocument:
        case QXmlStreamReader::EndElement:
            continue;
        default:;
        }

        if(xml.isStartElement())
        {
            qDebug("é start element");


// identifica as tag


            if(xml.name().toString()== "ChatText")
            {
                xml.readNext();
                qDebug(qPrintable(xml.text().toString()));

                emit chatText(xml.text().toString());
                return;
            }

            if(xml.name().toString()== "Element")
            {
                atrs = xml.attributes();
                xml.readNext();

                QUuid uuid = atrs.value("uuid").toString();
                QString type = atrs.value("type").toString();
                QString data = xml.text().toString();

                // TODO: receber arquivos
                emit elementReceived(uuid, type, data, QString());
                //chamar a função que bloqueia a edição do objeto cujo id é  xml.text().toString();
                return;

            }




            if(xml.name().toString()== "LockElement")
            {
                atrs = xml.attributes();
                xml.readNext();
                QUuid uuid = atrs.value("uuid").toString();

                emit lockElement(uuid);
                continue;

            }

            if(xml.name().toString()== "UnlockElement")
            {
                atrs = xml.attributes();
                xml.readNext();
                QUuid uuid = atrs.value("uuid").toString();

                emit unlockElement(uuid);
                continue;

            }


            if(xml.name().toString()== "NewLink")
            {
                atrs = xml.attributes();
                xml.readNext();

                QUuid uuid1 = atrs.value("uuid1").toString();
                QUuid uuid2 = atrs.value("uuid2").toString();

                qDebug(qPrintable(uuid1 + " creating a link betwen " + uuid2));

                // TODO: receber arquivos
                emit linkReceived(uuid1, uuid2);
                //chamar a função que bloqueia a edição do objeto cujo id é  xml.text().toString();
                return;

            }







            if(xml.name().toString()== "ToucheObject")
            {
                xml.readNext();
                //chamar a função que adiciona/substitui ao arquivo.touche o objeto definido como xml.text().toString();
                qDebug(qPrintable(xml.text().toString()));
                continue;
            }



            if (!atrs.isEmpty())
            {
                for (int i = 0; i < atrs.size(); i++)
                {
                    atr = atrs.at(i);
                    qualifiedName = atr.name().toString();
                    std::cout <<"tem algo aqui! " << atrs.size() << std::endl;
                    std::cout << qPrintable(qualifiedName) << " = " << qPrintable(atrs.value(qualifiedName).toString()) << std::endl;
                    std::cout <<"fim!" << std::endl;
                }
            }



            outro = atrs.value(xml.qualifiedName().toString()).toString();

            //std::cout << atrs.hasAttribute("text") << std::endl;
            namespaceUri = xml.namespaceUri().toString();
            name = xml.name().toString();
            value = xml.qualifiedName().toString();
            text = xml.text().toString();  //corpo do texto
            //elementText = xml.readElementText(); //corpo do texto
            dtdName = xml.dtdName().toString();

            std::cout << "############" << std::endl;
            std::cout << "DTD Name " << qPrintable(dtdName) << std::endl;

            std::cout << "namespaceUri " << qPrintable(namespaceUri) << std::endl;
            std::cout << "outro " << qPrintable(outro) <<std::endl;
            std::cout << "name "<< qPrintable(name) << std::endl;
            std::cout << "value " << qPrintable(value) << std::endl;
            std::cout << "text " << qPrintable(text) << std::endl;
            std::cout << "############" << std::endl;

        }

        if(xml.isEndElement())
        {
            std::cout << "é end element" << std::endl;
        }




    }
}
